package com.mexico.inloancash.activity;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mexico.inloancash.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndiaWebViewActivity extends BaseActivity {

    @BindView(R.id.h5_webView)
    WebView webView;

    private int type;
    private String webUrl;//获取传过来的Url
    private String orderNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_view_webview_layout);
        ButterKnife.bind(this);
        india_init_WebView();
        type = getIntent().getIntExtra("type", -1);
        webUrl = getIntent().getStringExtra("web");
        orderNumber = getIntent().getStringExtra("orderno");

        if (type != -1 && type == 1) {
            webView.loadUrl(webUrl + "?AppName=gg");
            //http://adminmxwb.cmd2019.cn/appapi/login/product_h5?AppName=gg
        } else if (type != -1 && type == 2) {
            Log.d("loan", "" + webUrl + "?orderno=" + orderNumber);
            //webView.loadUrl(webUrl + "?orderno=" + orderNumber);

            String url = webUrl + "?orderno=" + orderNumber;
            try {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(uri);
                startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
       /* webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });*/
    }

    public void india_init_WebView() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // User settings


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // handler.cancel();// Android默认的处理方式，WebView变成空白页
                handler.proceed();  // 接受所有网站的证书
                // handleMessage(Message msg); // 进行其他处理
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setBlockNetworkImage(false);
        webSettings.setUseWideViewPort(true);



        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//关键点

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放
    }
}
